<template>
	<view>
		<!-- <uni-popup ref="descpopup">
			<view style="background-color: #FFFFFF;padding: 30rpx;width: 100%;border-radius: 20rpx;">
				<text class="text">{{powers_remark[powersType].desc}}</text>
			</view>
		</uni-popup> -->
		<uni-popup ref="powers" type="center" :isMaskClick="false">
			<view style="background-color: #FFFFFF;border-radius: 20rpx;padding: 20rpx;width: 540rpx;">
				<!-- <view class="flex flex-x-y">
					<image src="../../static/fgx/shouquan.png" style="width: 300rpx;height: 300rpx;"></image>
				</view> -->

				<view class="color-333333 fs-30 bold flex flex-x-y mt-30">
					{{powers_remark[powersType].title}}
				</view>

				<view class="color-999999 fs-26 center" style="padding: 10rpx 0;">
					{{powers_remark[powersType].desc}}
				</view>

				<view class="flex mt-5 flex-x-b">
					<view class="p-btn-cancal" @tap.stop="cancel">以后再说</view>
					<view class="p-btn-cancal p-btn-sure" @tap.stop="sure">允许</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		name: "powers",
		props: {

		},
		data() {
			return {
				powersType: 'POWERS_IMAGE', //权限
				powers_remark: {
					'POWERS_IMAGE': {
						title: '发个信想访问你的相册',
						desc: '为了你能正常选择图片，发个信需要向你申请存储或者相机权限，如果不允许，你将无法选取图片。'
					},
					'POWERS_CAMERA': {
						title: '发个信想访问你的相机',
						desc: '为了你能正常拍摄图片，发个信需要向你申请相机权限，如果不允许，你将无法拍摄。'
					},
					'POWERS_ADDRESS': {
						title: '发个信想访问你的地理位置',
						desc: '为了你能正常选择位置发送给好友，发个信需要向你申请位置权限，如果不允许，你将无选取地理位置。'
					},
					'POWERS_AUDIO': {
						title: '发个信想访问你的麦克风',
						desc: '为了你能正常发送语音消息，发个信需要向你申请麦克风权限，如果不允许，你将无法发送语音消息。'
					}
				}
			};
		},
		methods: {
			open(obj) {
				if (obj.powersType) {
					this.obj = obj;
					this.powersType = obj.powersType;
					// #ifdef H5
					this.obj && this.obj.success && this.obj.success();
					return;
					// #endif
					if (this.is_ios == true) {
						this.obj && this.obj.success && this.obj.success();
						return;
					}
					this.$refs.powers.open();
					// this.$refs.descpopup.open('top');
					// if (uni.getStorageSync(this.powersType)) {
					// 	this.obj && this.obj.success && this.obj.success();
					// } else {

					// 	this.$refs.powers.open();
					// 	this.$refs.descpopup.open('top');
					// }

				}
			},
			cancel() {
				this.$refs.powers.close();
				// this.$refs.descpopup.close();
			},
			sure() {
				this.$refs.powers.close();
				// this.$refs.descpopup.close();
				if (this.powersType == 'POWERS_IMAGE') {
					uni.setStorageSync('POWERS_IMAGE', true);
					this.obj && this.obj.success && this.obj.success();
				}
				if (this.powersType == 'POWERS_CAMERA') {
					// android.permission.CAMERA
					uni.setStorageSync('POWERS_CAMERA', true);
					this.obj && this.obj.success && this.obj.success();
				}
				if (this.powersType == 'POWERS_ADDRESS') {
					this.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION').then(res => {
						uni.setStorageSync('POWERS_ADDRESS', true);
						this.obj && this.obj.success && this.obj.success();
					}).catch(() => {
						uni.setStorageSync('POWERS_ADDRESS', true);
					});
				}
				if (this.powersType == 'POWERS_AUDIO') {
					this.requestAndroidPermission('android.permission.RECORD_AUDIO').then(res => {
						uni.setStorageSync('POWERS_AUDIO', true);
						this.obj && this.obj.success && this.obj.success();
					}).catch(() => {
						uni.setStorageSync('POWERS_AUDIO', true);
					});
				}
			},
			requestAndroidPermission(permissionID) {
				return new Promise((resolve, reject) => {
					plus.android.requestPermissions(
						[permissionID], // 理论上支持多个权限同时查询，但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
						function(resultObj) {
							var result = 0;
							for (var i = 0; i < resultObj.granted.length; i++) {
								var grantedPermission = resultObj.granted[i];
								console.log('已获取的权限：' + grantedPermission);
								result = 1
							}
							for (var i = 0; i < resultObj.deniedPresent.length; i++) {
								var deniedPresentPermission = resultObj.deniedPresent[i];
								console.log('拒绝本次申请的权限：' + deniedPresentPermission);
								result = 0
							}
							for (var i = 0; i < resultObj.deniedAlways.length; i++) {
								var deniedAlwaysPermission = resultObj.deniedAlways[i];
								console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
								result = -1
							}
							resolve(result);
							// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
							// if (result != 1) {
							// gotoAppPermissionSetting()
							// }
						},
						function(error) {
							console.log('申请权限错误：' + error.code + " = " + error.message);
							resolve({
								code: error.code,
								message: error.message
							});
						}
					);
				});
			}
		}
	}
</script>

<style lang="scss">
	.p-btn-cancal {
		height: 65rpx;
		width: 220rpx;
		border-radius: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #666666;
		background-color: #f1f1f1;
	}

	.p-btn-sure {
		background-color: $bgColor;
		color: #FFFFFF;
	}
</style>